/*
 * ICExamView.java
 */

package icexam;

import icexam.dao.UserDao;
import icexam.exam.ExamingPanel;
import icexam.exam.StudyPanel;
import icexam.user.AdminLogin;
import icexam.user.AdminMainP;
import icexam.user.AdminModifyPSWP;
import icexam.user.DataBaseSetPanel;
import icexam.user.ModifyLogInPSWP;
import icexam.user.UserBean;
import icexam.user.UserInfo;
import icexam.user.UserLogin;
import icexam.user.UserMainP;
import icexam.user.UserSearchP;
import java.awt.BorderLayout;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimerTask;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public class ICExamView extends FrameView {

    ICExamView() {
        this(null);
    }


    public void goToUserLogin(){
        mainP.removeAll();
        ul.initRoleSelect();
        mainP.add(ul, BorderLayout.CENTER);
        mainP.updateUI();
        
    }
    public void goToAdminLogin(){
        mainP.removeAll();
        al.initRoleSelect();
        mainP.add(al, BorderLayout.CENTER);
        mainP.updateUI();
    }
    UserMainP userMainP = null;
    AdminMainP adminMainP = null;
    public void setMainView(){
        
    }
    UserLogin ul = null;
    AdminLogin al = null;

    public ICExamView(SingleFrameApplication app) {
        super(app);

        initComponents();
        
        userP.setVisible(false);
//        timeP.setVisible(false);
        ul = new UserLogin(this);
        al = new AdminLogin(this);
        returnP.setVisible(false);
        mainP.add(ul, BorderLayout.CENTER);
//        mainP.add(ul);
        mainP.updateUI();
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = ICExamApp.getApplication().getMainFrame();
            aboutBox = new ICExamAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        ICExamApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        userP = new javax.swing.JPanel();
        welcomeP = new javax.swing.JPanel();
        welcome = new javax.swing.JLabel();
        welcomT = new javax.swing.JLabel();
        rushT = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        returnP = new javax.swing.JPanel();
        returnB = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        mainP = new javax.swing.JPanel();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(800, 620));
        mainPanel.setLayout(new java.awt.BorderLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(icexam.ICExamApp.class).getContext().getResourceMap(ICExamView.class);
        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setPreferredSize(new java.awt.Dimension(800, 120));
        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 2, 0));

        jLabel1.setBackground(resourceMap.getColor("jLabel1.background")); // NOI18N
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jLabel1.setOpaque(true);
        jLabel1.setPreferredSize(new java.awt.Dimension(800, 50));
        jPanel2.add(jLabel1);

        userP.setBackground(resourceMap.getColor("userP.background")); // NOI18N
        userP.setName("userP"); // NOI18N
        userP.setPreferredSize(new java.awt.Dimension(800, 30));
        userP.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        welcomeP.setBackground(resourceMap.getColor("welcomeP.background")); // NOI18N
        welcomeP.setName("welcomeP"); // NOI18N
        welcomeP.setPreferredSize(new java.awt.Dimension(330, 25));
        welcomeP.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 0));

        welcome.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        welcome.setText(resourceMap.getString("welcome.text")); // NOI18N
        welcome.setName("welcome"); // NOI18N
        welcome.setPreferredSize(new java.awt.Dimension(40, 25));
        welcomeP.add(welcome);

        welcomT.setBackground(resourceMap.getColor("welcomT.background")); // NOI18N
        welcomT.setText(resourceMap.getString("welcomT.text")); // NOI18N
        welcomT.setName("welcomT"); // NOI18N
        welcomT.setPreferredSize(new java.awt.Dimension(200, 25));
        welcomeP.add(welcomT);

        rushT.setBackground(resourceMap.getColor("rushT.background")); // NOI18N
        rushT.setFont(resourceMap.getFont("rushT.font")); // NOI18N
        rushT.setForeground(resourceMap.getColor("rushT.foreground")); // NOI18N
        rushT.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        rushT.setText(resourceMap.getString("rushT.text")); // NOI18N
        rushT.setName("rushT"); // NOI18N
        rushT.setPreferredSize(new java.awt.Dimension(40, 25));
        welcomeP.add(rushT);

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        welcomeP.add(jLabel5);

        userP.add(welcomeP);

        jButton1.setBackground(resourceMap.getColor("jButton1.background")); // NOI18N
        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setPreferredSize(new java.awt.Dimension(70, 25));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        userP.add(jButton1);

        jPanel2.add(userP);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jSeparator1.setPreferredSize(new java.awt.Dimension(800, 2));
        jPanel2.add(jSeparator1);

        returnP.setBackground(resourceMap.getColor("userP.background")); // NOI18N
        returnP.setName("returnP"); // NOI18N
        returnP.setPreferredSize(new java.awt.Dimension(800, 30));
        returnP.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 5, 0));

        returnB.setBackground(resourceMap.getColor("returnB.background")); // NOI18N
        returnB.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        returnB.setText(resourceMap.getString("returnB.text")); // NOI18N
        returnB.setName("returnB"); // NOI18N
        returnB.setPreferredSize(new java.awt.Dimension(70, 25));
        returnB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                returnBActionPerformed(evt);
            }
        });
        returnP.add(returnB);

        jPanel2.add(returnP);

        mainPanel.add(jPanel2, java.awt.BorderLayout.NORTH);

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new java.awt.BorderLayout());

        mainP.setBackground(resourceMap.getColor("mainP.background")); // NOI18N
        mainP.setName("mainP"); // NOI18N
        mainP.setLayout(new java.awt.BorderLayout());
        jPanel1.add(mainP, java.awt.BorderLayout.CENTER);

        mainPanel.add(jPanel1, java.awt.BorderLayout.CENTER);

        statusPanel.setBackground(resourceMap.getColor("statusPanel.background")); // NOI18N
        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 1084, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 914, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.getApplication().exit();
//        System.exit(0);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void returnBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_returnBActionPerformed
        this.returnP.setVisible(false);
        this.goToMain();
    }//GEN-LAST:event_returnBActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel mainP;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JButton returnB;
    private javax.swing.JPanel returnP;
    private javax.swing.JLabel rushT;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JPanel userP;
    private javax.swing.JLabel welcomT;
    private javax.swing.JLabel welcome;
    private javax.swing.JPanel welcomeP;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;

    public void goToMain(){
        mainP.removeAll();
        if(!user.isAdmin()){
            mainP.add(userMainP, BorderLayout.CENTER);
        }else{
            mainP.add(adminMainP,BorderLayout.CENTER);
        }
        mainP.updateUI();
    }
  
    public void goToUserMain(UserMainP userMainP) {
        this.userMainP = userMainP;
        mainP.removeAll();
        mainP.add(userMainP, BorderLayout.CENTER);
        mainP.updateUI();
    }

    public void goToUserMain(AdminMainP adminMainP) {
        this.adminMainP = adminMainP;
        mainP.removeAll();
        mainP.add(adminMainP,BorderLayout.CENTER);
        mainP.updateUI();
    }
    ExamingPanel examingPanel = null;
    public void setMainView(ExamingPanel examingPanel) {
//        this.userMainP = userMainP;
        mainP.removeAll();
//        al.initRoleSelect();
        this.examingPanel = examingPanel;
        mainP.add(examingPanel, BorderLayout.CENTER);
        mainP.updateUI();
    }
    
    java.util.Timer timer = null;
    TimeTast myTimeTask = null;
    long startTime = 0;
    public void studying() {
//        this.timeP.setVisible(true);
        timer = new java.util.Timer();
        myTimeTask = new TimeTast();
        startTime = System.currentTimeMillis();
        timer.schedule(myTimeTask, 3*1000, 60*1000);
    }

    

    private boolean submitExam = false;

    public boolean isSubmitExam() {
        return submitExam;
    }

    public void setSubmitExam(int value) {

        if(value>user.getBestScore()){
            UserDao dao = new UserDao();
            user.setBestScore(value);
            dao.updateScore(user);
        }        
        this.returnP.setVisible(true);
        this.submitExam = submitExam;
    }
    public UserBean getUser(){
        return user;
    }
   
//    public void timeUpGameOver(){
//        timeP.setVisible(false);
//        this.returnP.setVisible(true);
//        if(examingPanel!=null&&!examingPanel.isTestOver())
//            examingPanel.getResult();
//    }

    private UserBean user;
    public void setUser(UserBean user) {
        this.user = user;
        welcome.setText(user.getCeoName());        
        this.userP.setVisible(true);
        this.rushT.setText(String.valueOf(user.getSpentTime()));
        studying();
    }

    public void goToUserInfo(UserInfo userInfo) {
        userInfo.setUser(this.user);
        mainP.removeAll();
        mainP.add(userInfo, BorderLayout.CENTER);
        mainP.updateUI();
        this.returnP.setVisible(true);
    }


   

    public void goToStudyPanel(StudyPanel studyPanel) {
        mainP.removeAll();
        mainP.add(studyPanel, BorderLayout.CENTER);
        mainP.updateUI();
        this.returnP.setVisible(true);
    }


    public void goToUserInfo(UserSearchP userSearchP) {
        mainP.removeAll();
        mainP.add(userSearchP, BorderLayout.CENTER);
        mainP.updateUI();
        this.returnP.setVisible(true);
    }
    public int getRushMinisTime(){
        return user.getSpentTime();
    }
    public void goToModifyPSWP(AdminModifyPSWP adminModifyPSWP) {
        mainP.removeAll();
        mainP.add(adminModifyPSWP, BorderLayout.CENTER);
        mainP.updateUI();
        this.returnP.setVisible(true);
    }

    public void goToDBSet(DataBaseSetPanel dataBaseSetPanel) {
        mainP.removeAll();
        mainP.add(dataBaseSetPanel, BorderLayout.CENTER);
        mainP.updateUI();
        this.returnP.setVisible(true);
    }

    public void goToModifyPSWP(ModifyLogInPSWP modifyLogInPSWP) {
        mainP.removeAll();
        mainP.add(modifyLogInPSWP, BorderLayout.CENTER);
        mainP.updateUI();
        this.returnP.setVisible(true);
    }




    class TimeTast extends TimerTask{
        UserDao dao = new UserDao();
        public void run(){
             long newTime = System.currentTimeMillis();
             int spent = (int) ((newTime - startTime) / 60000);
             if(spent>1){
                 startTime = newTime;
                 user.setSpentTime(user.getSpentTime()+spent);
                 rushT.setText(String.valueOf(user.getSpentTime()));
                 dao.updateTime(user);
                 System.out.println("one mins again : spent now = "+spent);
             }
//             if(rushMinisTime>=60||submitExam){
//                timeUpGameOver();
////                 this.cancel();
//             }
        }
    }
}
